<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="600px" @confirm="handleSubmit"
            @close="handleClose">
            <div class="chatList" v-infinite-scroll="load">
                <div class="chatItem me">
                    <div class="chatItem-header">
                        <div class="chatItem-avatar"><el-avatar :size="32" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" /></div>
                        <div class="chatItem-username">用户昵称</div>
                    </div>
                    <div class="chatItem-body">
                        <div class="chatItem-text">消息内容消息内容</div>
                    </div>
                </div>
                <div class="chatItem" v-for="i in count" :key="i">
                    <div class="chatItem-header">
                        <div class="chatItem-avatar"><el-avatar :size="32" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" /></div>
                        <div class="chatItem-username">智能体名称</div>
                    </div>
                    <div class="chatItem-body">
                        <div class="chatItem-text">消息内容消息内容</div>
                    </div>
                </div>
            </div>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictOptions } from '@/hooks/useDictOptions'
import { adminAdd, adminDetail, adminEdit } from '@/api/employee'
import { roleAll } from '@/api/perms/role'
import { companyListAll } from '@/api/company'
import { deptAll } from '@/api/org/department'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑员工' : '新增员工'
})

const count = ref(0)
const load = () => {
  count.value += 2
}

const formData = reactive({
    id: '',
    account: '',
    phone: '',
    nick_name: '',
    avatar: '',
    disable: 0,
    multipoint_login: 0,
    password: '',
    password_confirm: '',
    ac_id: '',
    role_id: [],
    name: ''
})

const checkPhone = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('手机号不能为空'))
    } else {
        //验证手机号
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        if (reg.test(value)) {
            callback()
        } else {
            return callback(new Error('请输入正确的联系电话'))
        }
    }
}

const formRules = reactive({
    account: [
        {
            required: true,
            message: '请输入账号',
            trigger: ['blur']
        }
    ],
    password: [
        {
            required: true,
            message: '请输入密码',
            trigger: ['blur']
        }
    ] as any[],
    password_confirm: [
        {
            required: true,
            message: '请输入确认密码',
            trigger: ['blur']
        },
        {
            validator: '',
            trigger: 'blur'
        }
    ] as any[],
    nick_name: [
        {
            required: true,
            message: '请输入昵称',
            trigger: ['blur']
        }
    ],
    role_id: [
        {
            type: 'array',
            required: true,
            message: '请选择角色',
            trigger: ['blur']
        }
    ],
    ac_id: [
        {
            type: 'number',
            required: true,
            message: '请选择所属企业',
            trigger: ['blur']
        }
    ]
})

const { optionsData } = useDictOptions<{
    role: any[]
    companyList: any[]
}>({
    role: {
        api: roleAll
    },
    companyList: {
        api: companyListAll
    }
    // dept: {
    //     api: deptAll
    // }
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formRules.password = []
    formRules.password_confirm = [
        {
            validator: passwordConfirmValidator,
            trigger: 'blur'
        }
    ]
    const data = await adminDetail({
        id: row.id
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}
const passwordConfirmValidator = (rule: object, value: string, callback: any) => {
    if (formData.password) {
        if (!value) callback(new Error('请再次输入密码'))
        if (value !== formData.password) callback(new Error('两次输入密码不一致!'))
    }
    callback()
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>

<style>
.chatList {
    background-color: #f9fafb;
    box-sizing: border-box;
    border-radius: 8px;
    padding: 16px;
    overflow-y: scroll;
    max-height: 50vh;
}

.chatItem {}

.chatItem-header {
    display: flex;
    align-items: center;
    padding: 10px 0;
}

.chatItem.me .chatItem-header {
    justify-content: flex-end;
    flex-wrap: wrap-reverse;
}

.chatItem-avatar {
    margin-right: 10px;
}

.chatItem-username {
    font-size: 14px;
}

.chatItem.me .chatItem-header .chatItem-avatar {
    order: 2;
    margin-right: 0;
    margin-left: 10px;
}

.chatItem.me .chatItem-header .chatItem-username {
    order: 1;
}

.chatItem-body {
    padding: 8px;
    background-color: #F2F4F7;
    border-radius: 8px;
    max-width: 70%;
}

.chatItem.me .chatItem-body {
    margin-left: auto;
    background-color: #ffffff;

}

.chatItem-text {
    font-size: 14px;
}
</style>